package com.kgc.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.dao.TbItemDescMapper;
import com.kgc.dao.TbItemMapper;
import com.kgc.entity.TbItem;
import com.kgc.entity.TbItemDesc;
import com.kgc.entity.TbItemExample;
import com.kgc.jedis.JedisClient;
import com.kgc.service.ItemService;
import com.kgc.utils.IDUtils;
import com.kgc.utils.JsonUtils;
import com.kgc.utils.ResultTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.jms.*;
import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private TbItemMapper tbItemMapper;
    @Autowired
    private TbItemDescMapper tbItemDescMapper;
    @Autowired
    private JmsTemplate jmsTemplate;
    @Autowired
    private Destination destination;
    @Autowired
    private JedisClient jedisClient;

    @Value("${ITEMID}")
    private String ITEMID;

    @Override
    public ResultTable findAll(int page, int limit, Long cid) {
        PageHelper.startPage(page,limit);
        TbItemExample example=new TbItemExample();
        if(cid!=null&&cid>0){
            TbItemExample.Criteria criteria = example.createCriteria();
            criteria.andCidEqualTo(cid);
        }
        List<TbItem> list=tbItemMapper.selectByExample(example);
        PageInfo pageInfo=new PageInfo(list);
        ResultTable resultTable=new ResultTable();
        resultTable.setCode(0);
        resultTable.setMsg("ok");
        resultTable.setCount(pageInfo.getTotal());
        resultTable.setData(pageInfo.getList());
        return  resultTable;
    }

    @Override
    public void addItem(TbItem item, String content1) {
       final long id = IDUtils.genItemId();
        item.setId(id);
        item.setStatus((byte) 1);
        item.setCreated(new Date());
        item.setUpdated(new Date());
        tbItemMapper.insert(item);
        //向商品描述库中添加商品
        TbItemDesc tbItemDesc=new TbItemDesc();
        tbItemDesc.setItemId(id);
        tbItemDesc.setItemDesc(content1);
        tbItemDesc.setCreated(new Date());
        tbItemDesc.setUpdated(new Date());
        tbItemDescMapper.insert(tbItemDesc);

        //将商品编号发到mq中
        jmsTemplate.send(destination, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage=session.createTextMessage(id+"");
                System.out.println("send"+textMessage.getText());
                return textMessage;
            }
        });


    }

    @Override
    public TbItem findById(Long id) {
        String jsonString =null;
        TbItem tbItem =null;
        if (jedisClient.exists(ITEMID+id)){
            jsonString= jedisClient.get(ITEMID+id);
            tbItem= JsonUtils.jsonToPojo(jsonString,TbItem.class);
        }else {
            tbItem=tbItemMapper.selectByPrimaryKey(id);
            jsonString=JsonUtils.objectToJson(tbItem);
            jedisClient.set(ITEMID+id,jsonString);
            jedisClient.expire(ITEMID+id,60*60);
        }
        return tbItem;
    }
}
